{extend name="public/base" /}

{block name="title"}管理员{/block}
{block name="body"}

	<div class="layui-fluid" id="component-tabs">
		<div class="layui-card">
			<div class="layui-card-body">
				<div class="layui-tab layui-tab-brief" lay-filter="server-tabs-brief"  lay-allowclose="true">
					<ul class="layui-tab-title">
						<li lay-id="vip" class="layui-this">用户等级</li>
					</ul>
					<div class="layui-tab-content">
						<div class="layui-tab-item layui-show">
							<div class="layui-tab-content" style="padding: 20px 0;">

								<div class="layui-form">
									<div class="layui-form-item">
										<div class="layui-inline">
										<label class="layui-form-label">用户积分</label>
											<div class="layui-input-inline" style="width: 120px;">
												<select name="vip">
													<option value="0">普通</option>
													<option value="1">L1</option>
													<option value="2">L2</option>
													<option value="3">L3</option>
													<option value="4">L4</option>
													<option value="5">L5</option>
												</select>
											</div>
											
											<div class="layui-input-inline">
												<input type="text" name="score" lay-verify="required" placeholder="积分区间:0-99" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
												<input type="text" name="nick" lay-verify="required" placeholder="认证名称" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
												<input type="text" name="postnum" lay-verify="required|number" placeholder="免费发帖日限额" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
												<input type="text" name="refreshnum" lay-verify="required|number" placeholder="免费刷新日限额" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
												<input type="text" name="postpoint" lay-verify="required|number" placeholder="发帖扣积分" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
												<input type="text" name="refreshpoint" lay-verify="required|number" placeholder="刷贴扣积分" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-input-inline">
											<input type="submit" class="pear-btn  pear-btn-primary" lay-submit lay-filter="vip-rule-submit" id="vip-rule-submit" value="立即提交">								
											</div>
											
										</div>	
									</div>
								</div>	
								<div class="layui-form-item">
									<table id="vip-rule" lay-filter="vip-rule"></table>
									<script type="text/html" id="vip-rule-bar">
										<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
										<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
									</script>
								</div>	
							</div>
						</div>

						{:hook('signadminhook')}

					</div>
				</div>
			</div>
		</div>
	</div>
  
	{__block__}

	<script>
		layui.use(['form'], function(){
			var $ = layui.jquery
			,element = layui.element
			,table = layui.table
			,form = layui.form;

			let VIP_RULE = "{:url('user.vip/list')}";

			// 渲染 tab 组件
  			element.render('tab', 'server-tabs-brief');

			//Vip规则
			table.render({
				elem: '#vip-rule',
				url: VIP_RULE,
				cols:[[
					{type: 'numbers', fixed: 'left'},
					{field: 'vip',title: '等级'},
					{field: 'score',title: '积分'},
					{field: 'nick',title: '认证'},
					{field: 'postnum',title: '免费发帖/日'},
					{field: 'refreshnum',title: '免费刷新/日'},
					{field: 'postpoint',title: '发帖扣积分'},
					{field: 'refreshpoint',title: '刷新扣积分'},
					{field: 'ctime',title: '时间'},
					{title: '操作', width: 150, align:'center', toolbar: '#vip-rule-bar'}
					
				]]
				,page: true
				,limit: 10
				,height: 'full-220'
				,text: '对不起，加载出现异常！'
			});

			table.on('tool(vip-rule)', function(obj) {
				if (obj.event === 'remove') {
					window.remove(obj);
				} else if (obj.event === 'edit') {
					window.edit(obj);
				}
			});
			
			//添加VIP规则
			form.on('submit(vip-rule-submit)',function(data){
				var field = data.field;
					$.ajax({
						type:"post",
						url:"{:url('user.vip/add')}",
						data:field,
						daType:"json",
						success:function (res){
							if (res.code == 0) {
								layer.msg(res.msg,{
									icon:6,
									time:2000
								}, function(){
								table.reload('vip-rule'); //数据刷新
								});
							} else {
								layer.open({
									title:'添加失败',
									content:res.msg,
									icon:5,
									anim:6
								});
							}
						}
					});
				return false;
			});


			window.edit = function(obj) {
				console.log(obj)
				layer.open({
					type: 2,
					title: '修改',
					shade: 0.1,
					area: ['450px', '580px'],
					content: 'edit.html?id=' + obj.data.id
				});
			}

			window.remove = function(obj) {
				
				layer.confirm('确定要删除?', {
					icon: 3,
					title: '提示'
				}, function(index) {
					layer.close(index);
					let loading = layer.load();
					$.ajax({
						url: "{:url('user.vip/delete')}?id=" + obj.data['id'],
						dataType: 'json',
						type: 'delete',
						success: function(result) {
							layer.close(loading);
							if (result.code === 0) {
								layer.msg(result.msg, {
									icon: 1,
									time: 1000
								}, function() {
									obj.del();
								});
							} else {
								layer.msg(result.msg, {
									icon: 2,
									time: 1000
								});
							}
						}
					})
				});
			}
				
		});
	</script>

{/block}